<template>
  <view class="network-diagnostic" :class="responsiveClass" :style="getContainerStyle('md')">
    <view class="header">
      <text class="title">网络诊断工具</text>
      <text class="subtitle">检测小程序网络连接问题</text>
    </view>
    
    <view class="diagnostic-section">
      <view class="section-title">网络状态检测</view>
      <view class="status-card">
        <view class="status-item">
          <text class="status-label">网络类型:</text>
          <text class="status-value" :class="networkStatus.type">{{ networkStatus.typeText }}</text>
        </view>
        <view class="status-item">
          <text class="status-label">连接状态:</text>
          <text class="status-value" :class="networkStatus.connected ? 'success' : 'error'">
            {{ networkStatus.connected ? '已连接' : '未连接' }}
          </text>
        </view>
      </view>
    </view>
    
    <view class="diagnostic-section">
      <view class="section-title">API连接测试</view>
      <view class="test-card">
        <view class="test-item">
          <text class="test-label">基础连接测试</text>
          <button class="test-btn" @click="testBasicConnection" :disabled="testing">
            {{ testing ? '测试中...' : '开始测试' }}
          </button>
        </view>
        <view class="test-result" v-if="testResult">
          <text class="result-label">测试结果:</text>
          <text class="result-value" :class="testResult.success ? 'success' : 'error'">
            {{ testResult.message }}
          </text>
        </view>
      </view>
    </view>
    
    <view class="diagnostic-section">
      <view class="section-title">域名配置检测</view>
      <view class="domain-card">
        <view class="domain-item">
          <text class="domain-label">当前API域名:</text>
          <text class="domain-value">http://api-rag.monkeymeerkat.cn/api</text>
        </view>
        <view class="domain-item">
          <text class="domain-label">需要配置的域名:</text>
          <text class="domain-value">https://api-rag.monkeymeerkat.cn</text>
        </view>
        <view class="domain-tip">
          <text class="tip-title">⚠️ 域名配置说明:</text>
          <text class="tip-content">• 小程序只能访问已配置的域名</text>
          <text class="tip-content">• 必须使用HTTPS协议</text>
          <text class="tip-content">• 不能包含端口号和路径</text>
          <text class="tip-content">• 开发阶段可勾选"不校验合法域名"</text>
        </view>
      </view>
    </view>
    
    <view class="diagnostic-section">
      <view class="section-title">常见问题解决</view>
      <view class="solutions-card">
        <view class="solution-item">
          <text class="solution-title">1. 检查网络连接</text>
          <text class="solution-desc">确保设备已连接到WiFi或移动网络</text>
        </view>
        <view class="solution-item">
          <text class="solution-title">2. 检查域名配置</text>
          <text class="solution-desc">确认后端API域名已在小程序后台配置</text>
        </view>
        <view class="solution-item">
          <text class="solution-title">3. 检查HTTPS协议</text>
          <text class="solution-desc">小程序要求API必须使用HTTPS协议</text>
        </view>
        <view class="solution-item">
          <text class="solution-title">4. 检查防火墙设置</text>
          <text class="solution-desc">确保网络防火墙未阻止API请求</text>
        </view>
      </view>
    </view>
    
    <view class="action-section">
      <button class="refresh-btn" @click="refreshDiagnostic">
        刷新诊断
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import http from '@/utils/request.js'

const networkStatus = ref({
  type: 'unknown',
  typeText: '未知',
  connected: false
})

const testResult = ref(null)
const testing = ref(false)

// 检查网络状态
const checkNetworkStatus = () => {
  uni.getNetworkType({
    success: (res) => {
      console.log('网络类型:', res.networkType)
      networkStatus.value.type = res.networkType
      networkStatus.value.connected = res.networkType !== 'none'
      
      switch (res.networkType) {
        case 'wifi':
          networkStatus.value.typeText = 'WiFi网络'
          break
        case '2g':
          networkStatus.value.typeText = '2G网络'
          break
        case '3g':
          networkStatus.value.typeText = '3G网络'
          break
        case '4g':
          networkStatus.value.typeText = '4G网络'
          break
        case '5g':
          networkStatus.value.typeText = '5G网络'
          break
        case 'none':
          networkStatus.value.typeText = '无网络'
          break
        default:
          networkStatus.value.typeText = '未知网络'
      }
    },
    fail: (err) => {
      console.error('获取网络状态失败:', err)
      networkStatus.value.type = 'error'
      networkStatus.value.typeText = '获取失败'
      networkStatus.value.connected = false
    }
  })
}

// 测试基础连接
const testBasicConnection = async () => {
  testing.value = true
  testResult.value = null
  
  try {
    const result = await http.testConnection()
    testResult.value = {
      success: result.success,
      message: result.message
    }
  } catch (error) {
    console.error('连接测试失败:', error)
    testResult.value = {
      success: false,
      message: error.message || '连接测试失败'
    }
  } finally {
    testing.value = false
  }
}

// 刷新诊断
const refreshDiagnostic = () => {
  checkNetworkStatus()
  testResult.value = null
}

// 页面加载时执行诊断
onMounted(() => {
  checkNetworkStatus()
})
</script>

<style scoped>
.network-diagnostic {
  padding: 40rpx;
  background: #f8f9fa;
  min-height: 100vh;
  height: 100vh;
  width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}

.header {
  text-align: center;
  margin-bottom: 60rpx;
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  color: #2c3e50;
  display: block;
  margin-bottom: 20rpx;
}

.subtitle {
  font-size: 28rpx;
  color: #7f8c8d;
  display: block;
}

.diagnostic-section {
  margin-bottom: 40rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 20rpx;
}

.status-card, .test-card, .solutions-card {
  background: white;
  border-radius: 20rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.status-item, .test-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.status-item:last-child, .test-item:last-child {
  margin-bottom: 0;
}

.status-label, .test-label {
  font-size: 28rpx;
  color: #2c3e50;
  font-weight: 500;
}

.status-value {
  font-size: 28rpx;
  font-weight: 600;
}

.status-value.success {
  color: #27ae60;
}

.status-value.error {
  color: #e74c3c;
}

.status-value.wifi {
  color: #3498db;
}

.status-value.2g, .status-value.3g, .status-value.4g, .status-value.5g {
  color: #f39c12;
}

.test-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 10rpx;
  padding: 15rpx 30rpx;
  font-size: 26rpx;
  font-weight: 500;
}

.test-btn:disabled {
  background: #bdc3c7;
}

.test-result {
  margin-top: 20rpx;
  padding-top: 20rpx;
  border-top: 1rpx solid #ecf0f1;
}

.result-label {
  font-size: 26rpx;
  color: #7f8c8d;
  display: block;
  margin-bottom: 10rpx;
}

.result-value {
  font-size: 28rpx;
  font-weight: 600;
  display: block;
}

.result-value.success {
  color: #27ae60;
}

.result-value.error {
  color: #e74c3c;
}

.solution-item {
  margin-bottom: 30rpx;
}

.solution-item:last-child {
  margin-bottom: 0;
}

.solution-title {
  font-size: 28rpx;
  font-weight: 600;
  color: #2c3e50;
  display: block;
  margin-bottom: 10rpx;
}

.solution-desc {
  font-size: 26rpx;
  color: #7f8c8d;
  line-height: 1.5;
  display: block;
}

.action-section {
  margin-top: 60rpx;
}

.refresh-btn {
  width: 100%;
  height: 90rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 20rpx;
  font-size: 32rpx;
  font-weight: 600;
  box-shadow: 0 8rpx 25rpx rgba(102, 126, 234, 0.3);
}

/* 响应式断点样式 */
/* 平板断点 */
@media (min-width: 481px) {
  .network-diagnostic {
    max-width: 480px;
    margin: 0 auto;
  }
  
  .diagnostic-section {
    padding: 0 40rpx;
  }
  
  .title {
    font-size: 48rpx;
  }
  
  .subtitle {
    font-size: 32rpx;
  }
}

/* 桌面中等断点 */
@media (min-width: 769px) {
  .network-diagnostic {
    max-width: 520px;
    margin: 0 auto;
  }
  
  .diagnostic-section {
    padding: 0 60rpx;
  }
  
  .header {
    margin-bottom: 80rpx;
  }
  
  .title {
    font-size: 56rpx;
  }
  
  .subtitle {
    font-size: 36rpx;
  }
  
  .section-title {
    font-size: 40rpx;
  }
}

/* 桌面大屏断点 */
@media (min-width: 1025px) {
  .network-diagnostic {
    max-width: 560px;
    margin: 0 auto;
  }
  
  .diagnostic-section {
    padding: 0 80rpx;
  }
  
  .refresh-btn {
    height: 100rpx;
    font-size: 36rpx;
  }
}
</style> 
 